<?php

namespace app\api\controller;

use \app\api\model\Student as StudentModel;

class Student
{
    public function __construct()
    {
        //判断token是否有效，通过token读取学生的所有信息
        $token = input('get.token');
        if (empty($token) || $token == '-1') output_error('token为空');
        $this->studentData = StudentModel::where('student_token', '=', $token)->find();
        if (empty($this->studentData)) output_error('token无效，请重新登录');
    }

    //注册功能
    public function getData()
    {
        //输出学生的所有信息，去除“student_password”字段
        unset($this->studentData['student_password']);
        unset($this->studentData['student_token']);
        output_data($this->studentData);
    }

    public function modifyName()
    {
        //修改学生姓名，学生姓名可重复。
        //通过学号，姓名重置密码http://127.0.0.1/api/base/findpass?no=20190506227&password=1234567&name=%E9%BB%84%E5%85%88%E4%B9%90
        $name = input('get.name');
        if (empty($name)){
            output_error('信息不完整');
        }
        StudentModel::where('student_id','=',$this->studentData['student_id'])->update(array('student_name'=>$name));
        output_data(1);
    }

    public function modifyAvatar()
    {
        //修改学生头像，需要自行创建文件夹，文件系统。
        //第一步：上传文件拿到文件名
        $file = request()->file('image');
        $name = Filesystem::disk('avatar')->putFile('', $file, 'uniqid');
        //第二步：删除原有文件
        if ($this->studentData['student _avatar'] != 'default.png') {
            $filename = app()->getRootPath() . 'public\\upload\\avatar\\' . $this->studentData['student_avatar'];
            unlink($filename);
        }
        //第三步：更新数据表
        MemberModel::where('member_id', '=', $this->memberId)->update(array('member_avatar' => $name));
        output_data(URL_AVATAR . $name);

        //学生头像文件唯一，更新时需要删除原有的文件。
    }

}
